package cn.nfwx.tv.adapter;

import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

import cn.nfwx.tv.R;
import cn.nfwx.tv.model.TvHistroy;
import cn.nfwx.tv.util.GlideUtil;

public class TvHistroyAdapter extends BaseQuickAdapter<TvHistroy, BaseViewHolder> {

    private boolean isDelete = false;
    private boolean isSelectAll = false;

    private OnCheckedChangeDelListener onCheckedChangeDelListener;

    public void setOnCheckedChangeDelListener(OnCheckedChangeDelListener onCheckedChangeDelListener) {
        this.onCheckedChangeDelListener = onCheckedChangeDelListener;
    }

    public interface OnCheckedChangeDelListener {
        // 是否全选中
        void onCheckedAllChanged(boolean stats);
    }

    public TvHistroyAdapter() {
        super(R.layout.fragment_history_item);
    }

    @Override
    protected void convert(BaseViewHolder helper, final TvHistroy item) {
        helper.setText(R.id.history_item_name, item.getProgram_name());
        helper.setText(R.id.history_precent, "已观看" + ((item.getPercent() > 100) ? 100 : item.getPercent()) + "%");
        GlideUtil.loadSimpleImageWithThumb(helper.itemView.getContext(), item.getSnap(), (ImageView) helper.getView(R.id.history_item_img));
        if (isDelete) {
            helper.setVisible(R.id.cb_history, true);
        } else {
            helper.setVisible(R.id.cb_history, false);
        }
        CheckBox cb_history = helper.getView(R.id.cb_history);
        cb_history.setOnCheckedChangeListener(null);
        cb_history.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                item.setCheck(!item.isCheck());
                if (onCheckedChangeDelListener != null) {
                    onCheckedChangeDelListener.onCheckedAllChanged(isDeleteAllDatas());
                }
            }
        });
        cb_history.setChecked(item.isCheck());
        if (onCheckedChangeDelListener != null) {
            onCheckedChangeDelListener.onCheckedAllChanged(isDeleteAllDatas());
        }
    }

    public void setDelete(boolean isDelete) {
        this.isDelete = isDelete;
        notifyDataSetChanged();
    }

    public boolean isDelete() {
        return isDelete;
    }

    public boolean isSelectAll() {
        return isSelectAll;
    }

    public void setSelectAll(boolean selectAll) {
        isSelectAll = selectAll;
        for (TvHistroy tvHistroy : getData()) {
            tvHistroy.setCheck(selectAll);
        }
        notifyDataSetChanged();
    }

    /**
     * 获取删除的列表
     *
     * @return
     */
    public List<TvHistroy> getDeleteDatas() {
        List<TvHistroy> tvHistroys = new ArrayList<>();
        for (TvHistroy tvHistroy : getData()) {
            if (tvHistroy.isCheck()) {
                tvHistroys.add(tvHistroy);
            }
        }
        return tvHistroys;
    }

    /**
     * 获取删除的列表
     *
     * @return
     */
    public boolean isDeleteAllDatas() {
        boolean stats = true;
        for (TvHistroy tvHistroy : getData()) {
            if (!tvHistroy.isCheck()) {
                stats = false;
                break;
            }
        }
        isSelectAll = stats;
        return stats;
    }
}
